package top.zlhy7.lock;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.Vector;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * @author renyong
 * @date 2022/8/2 20:42
 * @description 线程不安全问题演示
 */
public class TheadDemo4 {
    /**
     * ArrayList线程不安全演示
     */
    public static void test1(){
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                //向集合添加内容
                list.add(UUID.randomUUID().toString().substring(8));
                //从集合获取内容
                /*
                主要是取这一步会报错
                Exception in thread "1" Exception in thread "2" java.util.ConcurrentModificationException
                 */
                System.out.println(list);
            },String.valueOf(i)).start();
        }
    }

    /**
     * 通过Vector解决
     */
    public static void test2(){
        Vector<String> list = new Vector<>();
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                //向集合添加内容
                list.add(UUID.randomUUID().toString().substring(8));
                //从集合获取内容
                /*
                主要是取这一步会报错
                Exception in thread "1" Exception in thread "2" java.util.ConcurrentModificationException
                 */
                System.out.println(list);
            },String.valueOf(i)).start();
        }
    }

    /**
     * 通过Collections解决
     */
    public static void test3(){
        List<String> list = Collections.synchronizedList(new ArrayList<>());
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                //向集合添加内容
                list.add(UUID.randomUUID().toString().substring(8));
                //从集合获取内容
                /*
                主要是取这一步会报错
                Exception in thread "1" Exception in thread "2" java.util.ConcurrentModificationException
                 */
                System.out.println(list);
            },String.valueOf(i)).start();
        }
    }

    /**
     * 首推这个方法，通过juc里面的CopyOnWriteArrayList
     * 写时并发操作
     */
    public static void test4(){
        List<String> list = new CopyOnWriteArrayList<>();
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                //向集合添加内容
                list.add(UUID.randomUUID().toString().substring(8));
                //从集合获取内容
                /*
                主要是取这一步会报错
                Exception in thread "1" Exception in thread "2" java.util.ConcurrentModificationException
                 */
                System.out.println(list);
            },String.valueOf(i)).start();
        }
    }

    /**
     * hashSet 线程不安全演示
     * 解决方案：
     * - CopyOnWriteArraySet
     * - Collections.synchronizedSet
     */
    public static void setTest5(){
        Set<String> set = new CopyOnWriteArraySet<>();
        for (int i = 0; i < 30; i++) {
            new Thread(()->{
                //向集合添加内容
                set.add(UUID.randomUUID().toString().substring(8));
                //从集合获取内容
                /*
                主要是取这一步会报错
                Exception in thread "1" Exception in thread "2" java.util.ConcurrentModificationException
                 */
                System.out.println(set);
            },String.valueOf(i)).start();
        }
    }
    /**
     * hashmap 线程不安全演示
     * 解决方案：
     * - ConcurrentHashMap
     * - Collections.synchronizedMap
     * - hashTable
     */
    public static void hashTest6(){
        Map<String,String> map = Collections.synchronizedMap(new HashMap<>());
        for (int i = 0; i < 30; i++) {
            String key = String.valueOf(i);
            new Thread(()->{
                //向集合添加内容
                map.put(key,UUID.randomUUID().toString().substring(8));
                //从集合获取内容
                /*
                主要是取这一步会报错
                Exception in thread "1" Exception in thread "2" java.util.ConcurrentModificationException
                 */
                System.out.println(map);
            },String.valueOf(i)).start();
        }
    }
    public static void main(String[] args) {
        hashTest6();
    }
}
